package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.content.Intent;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.dl7.recycler.listener.OnRecyclerViewItemLongClickListener;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyCheck1ListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.CheckListBean;
import patrol.mobile.com.mobilepatrol.bean.DictListBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by K on 2018/9/14.
 */
public class EmergencyCheck1Activity extends CommonListActivity {

    private ImageView ivSearch;
    private View itemTopView;
    private MySpinner spOrgName;
    private MySpinner spCheckType;
    private MySpinner spCheckWay;
    private OrgListBean orgListBean;
    private String orgId = "";
    private String checkCategory = "";
    private String checkType = "";
    private List<String> checkCategoryList = new ArrayList<>();
    private List<String> checkTypeList = new ArrayList<>();
    private List<CheckListBean> checkListBeanList = new ArrayList<>();
    private List<String> orgNameList = new ArrayList<>();
    private List<String> orgIdList = new ArrayList<>();

    @Override
    public void initView() {
        super.initView();
        itemTopView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_check1_top, null);
        spOrgName = itemTopView.findViewById(R.id.spOrgName);
        spCheckType = itemTopView.findViewById(R.id.spCheckType);
        spCheckWay = itemTopView.findViewById(R.id.spCheckWay);
        ivSearch = itemTopView.findViewById(R.id.ivsearch);
//        flContent.addView(itemTopView);

        spOrgName.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
        spCheckType.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
        spCheckWay.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
    }


    @Override
    public void initTitle() {
        super.initTitle();
        iv_right_title.setVisibility(View.VISIBLE);
        iv_right_title.setImageDrawable(getResources().getDrawable(R.mipmap.add_white));
        iv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EmergencyCheck1Activity.this, EmergencyCheckAddActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencyCheck1ListAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_CHECK_1;
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
//        List<String> list = new ArrayList<>();
//        for (int i = 0; i < 5; i++) {
//            list.add("123");
//        }
//        mAdapter.updateItems(list);
        getOrg();
//        getDictList("checkCategory");
//        getDictList("checkType");
    }

    private void getData() {
//        String orgNme = spOrgName.getmText().getText().toString();
//        checkCategory = spCheckType.getmText().getText().toString();
//        checkType = spCheckWay.getmText().getText().toString();
//        for (int i = 0; i < orgNameList.size(); i++) {
//            if (orgNme.equals(orgNameList.get(i))) {
//                orgId = orgIdList.get(i);
        requestData(orgId, checkCategory, checkType);
//                return;
//            }
//        }
    }

    @Override
    public void setListener() {
        super.setListener();
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                // startActivity(EmergencyEvent2Activity.class);
            }
        });


        tv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // startActivity(EmergencyCheck2Activity.class);
            }
        });


        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String orgNme = spOrgName.getmText().getText().toString();
                checkCategory = spCheckType.getmText().getText().toString();
                checkType = spCheckWay.getmText().getText().toString();
                for (int i = 0; i < orgNameList.size(); i++) {
                    if (orgNme.equals(orgNameList.get(i))) {
                        orgId = orgIdList.get(i);
                        requestData(orgId, checkCategory, checkType);
                        return;
                    }
                }
            }
        });

        mAdapter.setOnItemLongClickListener(new OnRecyclerViewItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View view, int position) {
                //长按删除
                CheckListBean adviceListBean = checkListBeanList.get(position);

                deleteData(adviceListBean.getId());

                return false;
            }
        });


    }

    private void deleteData(String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.IEMERGENCY_CHECK_DELETE);
        sender.requestPost("id", id, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                orgId = spOrgName.getmText().getText().toString();
                checkCategory = spCheckType.getmText().getText().toString();
                checkType = spCheckWay.getmText().getText().toString();
                requestData(orgId, checkCategory, checkType);
            }
        });
    }

    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                orgNameList = new ArrayList<>();
                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    orgNameList.add(listBean.getOrgName());
                    orgIdList.add(listBean.getId());
                }
                getDictList("checkCategory");

                if (orgNameList != null && orgNameList.size() > 0) {
                    spOrgName.getmText().setText(orgNameList.get(0));
                }
                spOrgName.setmDatas(orgNameList, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        // orgId = orgListBean.getList().get(postion).getId();
                        getData();
                    }
                });
            }
        });
    }


    private void requestData(String orgId, String checkCategory, String checkType) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.IEMERGENCY_CHECK_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", "");
        map.put("checkCategory", "");
        map.put("checkType", "");
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                checkListBeanList = GsonUtil.jsonToList(listArray.toString(), CheckListBean.class);
                mAdapter.updateItems(checkListBeanList);

            }
        });
    }


    private void getDictList(final String dictType) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.DICT_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("dictType", dictType);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                List<DictListBean> dictListBeanList = GsonUtil.jsonToList(listArray.toString(), DictListBean.class);
                if (dictType.equals("checkCategory")) {
                    for (int i = 0; i < dictListBeanList.size(); i++) {
                        checkCategoryList.add(dictListBeanList.get(i).getLabel());
                    }
                    spCheckType.setmDatas(checkCategoryList, new MySpinner.CallBack() {
                        @Override
                        public void onSelect(int postion) {
                            checkCategory = checkCategoryList.get(postion);
                            getData();
                        }
                    });

                    getDictList("checkType");

                } else if (dictType.equals("checkType")) {
                    for (int i = 0; i < dictListBeanList.size(); i++) {
                        checkTypeList.add(dictListBeanList.get(i).getLabel());
                    }
                    spCheckWay.setmDatas(checkTypeList, new MySpinner.CallBack() {
                        @Override
                        public void onSelect(int postion) {
                            checkType = checkTypeList.get(postion);
                            getData();
                        }
                    });

                    getData();
                }


            }
        });
    }

}
